package com.kingmed.kmss.common.vo.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingmed.kmss.common.vo.CommonPage;
import lombok.Data;

import java.util.List;

/**
 * 分页数据封装类
 */
@Data
public class ApiCommonPage<T> {
    private Integer pageNo;
    private Integer pageSize;
    private Integer totalPage;
    private Long total;
    private List<T> list;

    /**
     * mybatis Page转对外ApiCommonPage，带total
     * @param pageResult
     * @return
     * @param <T>
     */
    public static <T> ApiCommonPage<T> restPageWithTotal(Page<T> pageResult) {
        ApiCommonPage<T> result = new ApiCommonPage<>();
        result.setPageNo(Convert.toInt(pageResult.getCurrent()));
        result.setPageSize(Convert.toInt(pageResult.getSize()));
        result.setTotal(pageResult.getTotal());
        result.setTotalPage(Convert.toInt(pageResult.getPages()));
        result.setList(pageResult.getRecords());
        return result;
    }

    public static <T> ApiCommonPage<T> restPageWithTotal(Page<T> pageResult, boolean isCount) {
        ApiCommonPage<T> result = new ApiCommonPage<>();
        result.setPageNo(Convert.toInt(pageResult.getCurrent()));
        result.setPageSize(Convert.toInt(pageResult.getSize()));
        result.setTotal(isCount ? pageResult.getTotal() : -1);
        result.setTotalPage(Convert.toInt(pageResult.getPages()));
        result.setList(pageResult.getRecords());
        return result;
    }


    /**
     * mybatis Page转对外ApiCommonPage
     * 对外接口，接口规范要求不返回total
     * @param pageResult
     * @return
     * @param <T>
     */
    public static <T> ApiCommonPage<T> restPageWithoutTotal(Page<T> pageResult) {
        ApiCommonPage<T> result = new ApiCommonPage<>();
        result.setPageNo(Convert.toInt(pageResult.getCurrent()));
        result.setPageSize(Convert.toInt(pageResult.getSize()));
        result.setList(pageResult.getRecords());
        return result;
    }

    /**
     * CommonPage转对外ApiCommonPage
     * 对外接口，接口规范要求不返回total
     * @param pageResult
     * @return
     * @param <T>
     */
    public static <T> ApiCommonPage<T> restCommonPage(CommonPage<T> pageResult) {
        ApiCommonPage<T> result = new ApiCommonPage<>();
        result.setPageNo(Convert.toInt(pageResult.getPageNo()));
        result.setPageSize(Convert.toInt(pageResult.getPageSize()));
        result.setList(pageResult.getList());
        return result;
    }

}
